iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0

今天我們來實作個人化信件的部分
老樣子進入我們的程式碼環節

程式碼解析

function sendPersonalizedEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MailList");
  if (!sheet) {
    Logger.log("找不到 MailList 工作表!");
    return;
  }
  var lastRow = sheet.getLastRow();

SpreadsheetApp.getActiveSpreadsheet():取得目前開啟的 Google 試算表。
getSheetByName("MailList"):取得名稱為 MailList 的工作表。
如果工作表不存在,sheet 就是 null,用 if (!sheet) 檢查並中斷程式,避免出現 getLastRow 的 TypeError。
getLastRow():取得該工作表中最後有資料的列數,用來控制迴圈範圍。

  for (var i = 2; i <= lastRow; i++) {
    var emailAddress = sheet.getRange(i, 1).getValue();
    var name = sheet.getRange(i, 2).getValue();
    var task = sheet.getRange(i, 3).getValue();
    var date = sheet.getRange(i, 4).getValue();

從第 2 列開始 (i = 2),假設第 1 列是標題。
sheet.getRange(i, 1).getValue():取得第 i 列第 1 欄的資料(Email)。
同理,取得名字、任務、日期。
這樣每一列就對應一封要寄出的信件內容。

    var subject = "專屬提醒 - " + name;

    var htmlBody = `
      <div style="font-family: 'Arial', sans-serif; background: #f0f2f5; padding: 40px 20px;">
        <div style="background: #ffffff; border-radius: 14px; max-width: 650px; margin: auto; box-shadow: 0 8px 20px rgba(0,0,0,0.08); border: 1px solid #e0e4e8; overflow: hidden;">
          
          <!-- 卡片標題區 -->
          <div style="background: linear-gradient(90deg, #27ae60, #2ecc71); color: white; padding: 20px 30px;">
            <h2 style="margin: 0; font-size: 22px;">您好 ${name}</h2>
          </div>

          <!-- 內容區 -->
          <div style="padding: 30px;">
            <p style="font-size: 16px; line-height: 1.7; margin-bottom: 20px;">
              這是一封專屬於您的提醒,請查看以下任務資訊:
            </p>

            <!-- 任務表格 -->
            <table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">
              <tr>
                <td style="padding: 12px; background: #27ae60; color: white; font-weight: bold; border-radius: 6px 0 0 6px;">任務</td>
                <td style="padding: 12px; background: #ecf9f1; border-radius: 0 6px 6px 0;">${task}</td>
              </tr>
              <tr>
                <td style="padding: 12px; background: #27ae60; color: white; font-weight: bold; border-radius: 6px 0 0 6px;">日期</td>
                <td style="padding: 12px; background: #ecf9f1; border-radius: 0 6px 6px 0;">${date}</td>
              </tr>
            </table>

            <p style="font-size: 16px; line-height: 1.7;">
              請記得準時完成,祝您順利!
            </p>

            <hr style="border: none; border-top: 1px solid #dfe3e8; margin: 25px 0;">

            <p style="font-size: 14px; color: #7f8c8d; line-height: 1.5;">
              這是系統自動發送的提醒信件,請勿直接回覆。
            </p>
          </div>
        </div>
      </div>
    `;

    GmailApp.sendEmail(emailAddress, subject, "", {htmlBody: htmlBody});
  }
}

結果

https://ithelp.ithome.com.tw/upload/images/20250820/20169466thmSQk9DfV.jpg
https://ithelp.ithome.com.tw/upload/images/20250820/20169466imQ9CXdqjC.jpg


上一篇
D8 自動發信系統實作
下一篇
D10 團隊開會日曆
系列文
Google App Script雲端自動化與動態網頁實戰20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言